package org.csu.mypetstore.controller;

import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.CartItem;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@Controller
@RequestMapping("/cart")
@SessionAttributes(value = {"account", "order", "cart","userInfo"})
public class CartController {

    @Autowired
    CatalogService catalogService;
    
    //查看购物车
    @GetMapping("/viewItem")
    public String ViewCart( Model model) {
        Cart cart = (Cart) model.getAttribute("cart");
        if(cart==null) {
            cart = new Cart();
        }

        model.addAttribute("cart", cart);
        return "cart/Cart";
    }

    //添加商品进购物车
    @GetMapping("/addItemToCart")
    public String AddItemToCart( String workingItemId, Model model) {
        Cart cart = (Cart) model.getAttribute("cart");
        if(cart==null) {
            cart = new Cart();
        }
        if (workingItemId != null) {
            boolean isInStock = catalogService.isItemInStock(workingItemId);
            Item item = catalogService.getItem(workingItemId);

            cart.addItem(item,isInStock);
            model.addAttribute("cart", cart);
            return "cart/cart";
        } else return "common/error";
    }

    //从购物车中移除商品
    @GetMapping("/removeItemFromCart")
    public String RemoveItemFromCart( String workingItemId, Model model) {
        Cart cart = (Cart) model.getAttribute("cart");
        if(cart==null) {
            cart = new Cart();
        }
        if (workingItemId != null) {
            cart.removeItemById(workingItemId);
            model.addAttribute("cart", cart);
            return "cart/cart";
        } else return "common/error";
    }


    //更新购物车
    @PostMapping("/updateCartQuantities")
    public String UpdateCartQuantities(Model model) {
        Cart cart = (Cart) model.getAttribute("cart");
        if(cart==null) {
            cart = new Cart();
        }
        if (cart.getCartItemList().size() != 0) {
            for (CartItem cartItem : cart.getCartItemList()) {
                String itemId = cartItem.getItem().getItemId();
                try {
                    HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
                    int quantity1 = Integer.parseInt(request.getParameter(itemId));
                    cart.setQuantityByItemId(itemId,quantity1);
                    if (quantity1 < 1) {
                        cart.removeItemById(itemId);
                    }
                } catch (Exception e) {
                    model.addAttribute("message", "The Quantities of Item must be Integer!");
                    return "common/error";
                }
            }
        }
        model.addAttribute("cart", cart);
        return "cart/cart";
    }

    @GetMapping("/checkout")
    public String GoToCheckout( Model model) {
        Cart cart = (Cart) model.getAttribute("cart");
        if(cart==null) {
            cart = new Cart();
        }

        model.addAttribute("cart", cart);
        return "cart/Checkout";
    }
}
